package spring.webapp.anmeldesystem.persistence.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import spring.webapp.anmeldesystem.entity.Admin;
import spring.webapp.anmeldesystem.entity.Professor;
import spring.webapp.anmeldesystem.entity.Role;
import spring.webapp.anmeldesystem.entity.Student;
import spring.webapp.anmeldesystem.entity.User;
import spring.webapp.anmeldesystem.persistence.dao.AdminDao;
import spring.webapp.anmeldesystem.persistence.mapper.AdminMapper;
import spring.webapp.anmeldesystem.persistence.mapper.FachbereichMapper;
import spring.webapp.anmeldesystem.persistence.mapper.ProfessorMapper;
import spring.webapp.anmeldesystem.persistence.mapper.StudentMapper;

/**
 * @author Songhua Cui
 * 
 */
@Component
public class AdminDaoImpl implements AdminDao {

	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private ProfessorMapper professorMapper;
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private FachbereichMapper fachbereichMapper;

	@Override
	public void addStudent(Student student, String faculity) {
		long maxId = studentMapper.selectMaxIDFromStudent();
		long studentId = maxId + 1;
		student.setId(studentId);
		long faculityId = fachbereichMapper.selectFachbereichIdByName(faculity);
		student.setFachbereich_id(faculityId);
		studentMapper.insertStudent(student);
		fachbereichMapper.insertStudentInFachbereich(studentId, faculityId);
	}

	@Override
	public void updateStudent(Student student) {
	}

	@Override
	public void resetStudent(Student student) {
	}

	@Override
	public void addProfessor(Professor professor, String faculity) {
		long maxId = professorMapper.selectMaxIDFromProfessor();
		long id = maxId + 1;
		professor.setId(id);
		long faculityId = fachbereichMapper.selectFachbereichIdByName(faculity);
		professor.setFachbereich_id(faculityId);
		professorMapper.insertProfessor(professor);
		fachbereichMapper.insertProfessorInFachbereich(id, faculityId);
	}

	@Override
	public void updateProfessor(Professor professor) {
	}

	@Override
	public void resetProfessor(Professor professor) {
	}

	@Override
	public Admin getAdminByEmail(String email) {
		return adminMapper.selectAdminByEmail(email);
	}

	@Override
	public void addPassword(long id) {
	}

	@Override
	public List<User> getAllUsers() {
		List<User> users = new ArrayList<User>();
		// add role to admin
		List<Admin> admins = adminMapper.selectAdmins();
		for (Admin admin : admins) {
			admin.setRole(Role.ADMIN);
		}
		users.addAll(admins);

		// add role to professor
		List<Professor> professors = professorMapper.selectProfessors();
		for (Professor professor : professors) {
			professor.setRole(Role.PROFESSOR);
		}
		users.addAll(professors);

		// add role to student
		List<Student> students = studentMapper.selectStudents();
		for (Student student : students) {
			student.setRole(Role.STUDENT);
		}
		users.addAll(students);

		return users;
	}

	@Override
	public void resetPassword(Admin admin) {
		adminMapper.updateAdmin(admin);
	}

	@Override
	public void deleteAdmin(String email) {
	}

	@Override
	public void addAdmin(Admin admin) {
		adminMapper.insertAdmin(admin);
	}

}
